const { get, post } = require("../../utils/request");

// pages/shopManage/shopManage.js
Page({
  data: {
    code:'',
    scenicId: '',
    shopName:'',
    username:'',
    phone:'',
    count:'',
    array:[],
    index:0
  },
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    let that = this;
    wx.login({
      success(res) {
        that.setData({
          code: res.code
        })
      }
    })
    get('/scenic/area/scenicSearch',{scenicId: ''}).then(res => {
      //图片宽度自适应
      this.setData({
        array:res || []
      })
    })
  },
  addFn() {
    if(!this.data.scenicId){
      wx.showToast({
        icon:'none',
        title: '请选择所属景区',
      })
    }else if(!this.data.shopName){
      wx.showToast({
        icon:'none',
        title: '请输入商户名称',
      })
    }else if(!this.data.username){
      wx.showToast({
        icon:'none',
        title: '请输入经营者姓名',
      })
    }else if(!this.data.phone){
      wx.showToast({
        icon:'none',
        title: '请输入手机号',
      })
    }else if(!this.data.count){
      wx.showToast({
        icon:'none',
        title: '请输入需要的二维码数量',
      })
    }else{
      get('/user/getOpenId',{code: this.data.code}).then(res => {
        let data = {
          scenicId: this.data.scenicId,
          shopName: this.data.shopName,
          username: this.data.username,
          phone: this.data.phone,
          count: this.data.count,
          openid: res.openid
        }
        post('/shop/add',data).then(res => {
          wx.showToast({
            icon:'none',
            title: '商户信息提交成功，请等待管理员审核！',
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }, 1000);
        })
      })
    }
  },
  bindPickerChange(e){
    console.log(this.data.array[e.detail.value].scenicId,'------');
    this.setData({
      index:e.detail.value,
      scenicId: this.data.array[e.detail.value].scenicId
    })
  },
  shopNameFn(e){
    this.setData({
      shopName: e.detail.value
    })
  },
  usernameFn(e){
    this.setData({
      username: e.detail.value
    })
  },
  phoneFn(e){
    this.setData({
      phone: e.detail.value
    })
  },
  countFn(e){
    this.setData({
      count: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
})